import string
import random
from datetime import datetime
from django.http import HttpResponse
from django.utils.timezone import make_aware
from faker import Faker
from utils.random_time import get_time_result
from .models import Company


fake =Faker(locale='zh_CN')


def index(request):
    for _ in range(50):
        id_card = ''.join(random.sample(string.digits + string.ascii_uppercase, 30))
        company = Company(id_card=id_card, name=fake.company(), legal_person=fake.name(), address=fake.address(),
                          set_up_date=get_time_result())
        company.save()
    return HttpResponse('数据添加成功')


def search(request):
    start_date = make_aware(datetime(year=2022, month=1, day=28, hour=17, minute=12, second=30))
    end_date = make_aware(datetime(year=2022, month=1, day=28, hour=17, minute=12, second=36))
    companies = Company.objects.filter(create_time__range=(start_date, end_date))
    print(companies.query)
    for company in companies:
        print(company)
    return HttpResponse('2000年以后成立公司查询成功')
